#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv)
{
    if (argc != 3)
    {
        puts("testje nx1 nx2");
        return -1;
    }
    int m_Nx1 = atoi(argv[1]);
    int m_Nx2 = atoi(argv[2]);
    int st = 1;
    int tw = 2;
    int niv = 0;
    int nx1 = m_Nx1 + 1;
    int nx2 = m_Nx2 + 1;
    int nx12 = m_Nx1 + 2;
    int nx22 = m_Nx2 + 2;
//    ASSERT(nx22 >= tw, "tw < nx22 : " << tw << "is not smaller than" << nx22);

    int temptw = tw;
    int tempst = 0;
    int maxniv = 0;

    while (temptw < nx22)
    {
        maxniv += 2;
        tempst = temptw;
        temptw *= 2;
    }

    printf("max niv = %d\n", maxniv);

//    while (tempst > 2)
//    {
//        temptw = tempst;
//        tempst /= 2;
//        maxniv -= 2;
//    }

//    printf("max niv = %d\n", maxniv);

    for (int ii = st; ii <= nx1; ii += tw)
    {
        for (int jj = st; jj <= nx2; jj += tw)
        {
            // vul m_cse
        }
    }
    niv += 2;
    st = tw;
    tw *= 2;

    printf("voor  while1 st=%5d tw=%5d niv=%3d\n",st, tw, niv);

#ifdef WHILE
    while (tw < nx22)
#else
    for (int nv = niv; nv < maxniv; nv += 2)
#endif
    {
        for (int ii = tw; ii <= nx1; ii += tw)
        {
            for (int jj = st; jj <= nx2; jj += tw)
            {
                // vul m_cse
            }
        }

        for (int ii = st; ii <= nx1; ii += tw)
        {
            for (int jj = tw; jj <= nx2; jj += tw)
            {
                // vul m_cse
            }
        }

        for (int ii = st; ii <= nx1; ii += tw)
        {
            for (int jj = st; jj <= nx2; jj += tw)
            {
                // vul m_cse
            }
        }
        niv += 2;
        st = tw;
        tw *= 2;
        printf("eind  while1 st=%5d tw=%5d niv=%3d\n",st, tw, niv);
    } // while (tw < nx22)

#ifdef WHILE
    while (st > 2)
#else
    for (int nv = niv; nv > 2; nv -= 2)
#endif
    {
        printf("begin while2 st=%5d tw=%5d niv=%3d\n",st, tw, niv);
        tw = st;
        st /= 2;
        niv -= 2;
        for (int ii = st; ii <= nx1; ii += tw)
        {
            for (int jj = st; jj <= nx2; jj += tw)
            {
            }
        }
        // end of repeated code

        for (int ii = tw; ii <= nx1; ii += tw)
        {
            for (int jj = st; jj <= nx2; jj += tw)
            {
            }
        }

        for (int ii = st; ii <= nx1; ii += tw)
        {
            for (int jj = tw; jj <= nx2; jj += tw)
            {
            }
        }
    }

    tw = st;
    st /= 2;
    niv -= 2;
//    ASSERT(niv == 0,"not back where we started!");
    printf("na    while2 st=%5d tw=%5d niv=%3d\n",st, tw, niv);

}
    // end of repeated code
